package org.shock.ss.sms.service.system.impl;

import java.sql.SQLException;

import org.apache.commons.beanutils.BeanUtils;

import org.shock.ss.sms.bean.system.Dept;
import org.shock.ss.sms.commons.dao.BaseDao;
import org.shock.ss.sms.commons.db.ProxoolDbPool;
import org.shock.ss.sms.commons.db.TsDbTools;
import org.shock.ss.sms.commons.service.impl.BaseServiceImpl;
import org.shock.ss.sms.service.system.DeptService;

public class DeptServiceImpl extends BaseServiceImpl<Dept> implements DeptService {

	public DeptServiceImpl(BaseDao<Dept> dao) {
		super(dao);
	}

	@Override
	public void add(Dept dept) throws Exception {
		Long sjbmid = dept.getSjbmid();
		try {
			tx = ProxoolDbPool.getTranManager();
			tx.beginTransaction();
			Long newid = dao.addBean(dept).getId();
			String sjbmbh = TsDbTools.getString("select bmbh from " + Dept.getTable() + " where id=" + sjbmid);
			String sjbmqc = TsDbTools.getString("select bmqc from " + Dept.getTable() + " where id=" + sjbmid);
			dept.setId(newid);
			dept.setBmbh(sjbmbh + "$" + newid + "$");
			dept.setBmqc(sjbmqc + "." + dept.getBmmc());
			dao.update(dept);
			/** 更新上级部门leaf为false */
			TsDbTools.doSql("update " + Dept.getTable() + " set leaf=0 where id=" + sjbmid);
			tx.commitAndClose();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollbackAndClose();
			}
			throw e;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.shock.ss.sms.commons.service.impl.BaseServiceImpl#del(java.lang.String)
	 */
	@Override
	public void del(String sql) throws Exception {
		try {
			tx = ProxoolDbPool.getTranManager();
			tx.beginTransaction();
			super.del(sql);
			/** 更新没有子部门的部门leaf为true */
			dao.doSql("update " + Dept.getTable() + " d set leaf=1 where not exists (select * from " + Dept.getTable()
					+ " where sjbmid=d.id) ");
			tx.commitAndClose();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollbackAndClose();
			}
			throw e;
		}

	}

	@Override
	public void update(Dept dept) throws Exception {
		try {
			tx = ProxoolDbPool.getTranManager();
			tx.beginTransaction();
			Dept olddept = new Dept();
			BeanUtils.populate(olddept, dao.view("select * from " + Dept.getTable() + " where id=" + dept.getId()));
			Long sjbmid = dept.getSjbmid();
			Long id = dept.getId();
			String sjbmbh = TsDbTools.getString("select bmbh from " + Dept.getTable() + " where id=" + sjbmid);
			String sjbmqc = TsDbTools.getString("select bmqc from " + Dept.getTable() + " where id=" + sjbmid);
			int sjbmlev = TsDbTools.getInt("select lev from " + Dept.getTable() + " where id=" + sjbmid);
			String newbmbh = "";
			// 如果上级部门有变动则进行下级部门维护
			if (!dept.getSjbmid().equals(olddept.getSjbmid())) {
				if (sjbmlev == 0) {
					newbmbh = sjbmbh + dept.getBmbh();
				} else {
					newbmbh = sjbmbh + TsDbTools.NextKey("dept", "id");
				}
				if (dept.getSjbmid() == 34000000) {
					dept.setBmqc(dept.getBmmc());
					dept.setLev(0);
					dept.setBmbh(TsDbTools.NextKey("dept", "id") + "$" + id + "$");
				} else {
					dept.setBmqc(sjbmqc + "." + dept.getBmqc() == null ? "" : dept.getBmmc());
					dept.setLev(sjbmlev + 1);
					dept.setBmbh(newbmbh);
				}
				dao.update(dept);
				// 更新没有子部门的部门leaf为true
				dao.doSql("update dept d set leaf=1 where not exists (select * from " + Dept.getTable()
						+ " where sjbmid=d.id) ");
				// 更新下级所有子部门的部门部门编号和lev 部门全称
				String sql = "update dept set lev=" + dept.getLev() + "+1,bmbh='" + dept.getBmbh() + "'||substr(bmbh,"
						+ dept.getBmbh().length() + ",length(bmbh)) where id in (select id from " + Dept.getTable()
						+ " start with id=" + dept.getId() + " connect by prior id = sjbmid) and id!=" + dept.getId();
				try {
					dao.doSql(sql);
				} catch (SQLException e) {
					if (e.getErrorCode() == 1) {
						sql = "update dept set lev=" + dept.getLev() + "+1 where id in (select id from "
								+ Dept.getTable() + " start with id=" + dept.getId()
								+ " connect by prior id = sjbmid) and id!=" + dept.getId();
						dao.doSql(sql);
					} else {
						e.printStackTrace();
						throw e;
					}
				}
			} else {
				dao.update(dept);
			}

			// 更新下级部门的部门全称
			String sql = "update " + Dept.getTable()
					+ " d set bmqc=(select concat(a.bmqc,'.',d.bmmc) from (select id,bmqc from " + Dept.getTable()
					+ ")a where a.id=d.sjbmid) and lev>0";
			System.out.println(sql);
			dao.doSql(sql);
			/** 更新上级部门leaf为false */
			dao.doSql("update " + Dept.getTable() + " set leaf=0 where id=" + sjbmid);
			tx.commitAndClose();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollbackAndClose();
			}
			throw e;
		}
	}
}
